﻿///***************************************************************
/// 시  스  템 : Updms.Car
/// 단위시스템 : CAR
/// 계      층 : Presentation Layer
/// 프로그램명 : Car2012r
/// 파  일  명 : Car2012r.aspx.cs
/// 설      명 : 월별비용현황(주유비, 정비비)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-09-05 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;

using Updms.Car.Exe;
using Updms.Common.BaseClass;


namespace Updms.Car
{
	public partial class Car2012r : PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Car2012r";
		public static readonly string gs_path_info = "/updms/Car/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);

		//검색
		protected string kw1 = "";	// 차량ID
		protected string kw2 = "";	// 검색시작년월
		protected string kw3 = "";	// 검색종료년월
		protected string kw4 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetCarInfo();
			SetYearMonth();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion		

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_car_id.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_car_id.SelectedValue = kw1;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_from_year.SelectedValue +"-"+ ddl_search_from_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_from_year.SelectedValue = kw2.Substring(0, 4).ToString();
						ddl_search_from_month.SelectedValue = kw2.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_to_year.SelectedValue +"-"+ ddl_search_to_month.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_to_year.SelectedValue = kw3.Substring(0, 4).ToString();
						ddl_search_to_month.SelectedValue = kw3.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						ddl_search_chart_width.SelectedValue = kw4;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_car_id.SelectedValue;
					kw2 = ddl_search_from_year.SelectedValue +"-"+ ddl_search_from_month.SelectedValue;
					kw3 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					kw4 = ddl_search_chart_width.SelectedValue;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetCarInfo, SetYearMonth, SetChartWidth)

		//차량ID
		private void SetCarInfo()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			Car1010m_NT _Car1010m_NT = new Car1010m_NT();

			lo_dp.AddProperty("ls_use_yn", typeof(string), "Y");	//사용여부가 Y인것만

			lo_ds.Merge(_Car1010m_NT.GetCarInfoList(lo_dp));

			ddl_search_car_id.Items.Clear();
			ddl_search_car_id.DataSource = lo_ds;
			ddl_search_car_id.DataMember = "UPDMS_CAR_INFO";
			ddl_search_car_id.DataTextField = "Text";
			ddl_search_car_id.DataValueField = "Value";
			ddl_search_car_id.DataBind();
		}

		// 검색조건의 년월설정
		private void SetYearMonth()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			ddl_search_from_year.Items.Clear();
			ddl_search_to_year.Items.Clear();

			lo_dp.AddProperty("ls_car_id", typeof(string), ddl_search_car_id.SelectedValue);

			using (Car1010m_NT _Car1010m_NT = new Car1010m_NT())
			{
				lo_ds.Merge(_Car1010m_NT.GetMinNMaxExpenseDt(lo_dp));
			}

			if (DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_INFO", "Min_Dt_Y") > 0)
			{
				int li_min_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_INFO", "Min_Dt_Y");
				int li_max_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_INFO", "Max_Dt_Y");
				string ls_min_dt_m = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_INFO", "Min_Dt_M");
				string ls_max_dt_m = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_INFO", "Max_Dt_M");

				for (int i = li_min_dt_y; i <= li_max_dt_y; i++)
				{
					string ls_year = i.ToString();
					ddl_search_from_year.Items.Add(new ListItem(ls_year, ls_year));
					ddl_search_to_year.Items.Add(new ListItem(ls_year, ls_year));
				}

				//시작년도 선택
				if (WebControlUtil.CheckDdlValue(ddl_search_from_year, li_min_dt_y.ToString()))
				{
					ddl_search_from_year.SelectedValue = li_min_dt_y.ToString();
				}
				else
				{
					ddl_search_from_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년도
				}

				//종료년도 선택
				if (WebControlUtil.CheckDdlValue(ddl_search_to_year, li_max_dt_y.ToString()))
				{
					ddl_search_to_year.SelectedValue = li_max_dt_y.ToString();
				}
				else
				{
					ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년도
				}

				ddl_search_from_month.SelectedValue = ls_min_dt_m;
				ddl_search_to_month.SelectedValue = ls_max_dt_m;
			}
			else
			{
				ddl_search_from_year.Items.Add(new ListItem(DateUtil.GetYearEx(0, "yyyy"), DateUtil.GetYearEx(0, "yyyy")));
				ddl_search_to_year.Items.Add(new ListItem(DateUtil.GetYearEx(0, "yyyy"), DateUtil.GetYearEx(0, "yyyy")));
			}		
		}

		// 검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			ddl_search_from_year.SelectedValue = DateUtil.GetMonthEx(li_term, "yyyy"); //x개월전년도
			ddl_search_from_month.SelectedValue = DateUtil.GetMonthEx(li_term, "MM"); //x개월전월
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);
			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion	

		#region 챠트 (DrawMonthlyCost, SearchProcess)

		//조회
		private void DrawMonthlyCost()
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//초기화
			chart_monthly_cost.Data.Clear();

			//시리즈 갯수
			chart_monthly_cost.Data.Series = 5;

			//넓이
			chart_monthly_cost.Width = Convert.ToInt32(kw4);

			chart_monthly_cost.Series[0].MarkerShape = ChartFxUtil.markerShape(0);
			chart_monthly_cost.Series[1].MarkerShape = ChartFxUtil.markerShape(1);
			chart_monthly_cost.Series[2].MarkerShape = ChartFxUtil.markerShape(2);
			chart_monthly_cost.Series[3].MarkerShape = ChartFxUtil.markerShape(3);
			chart_monthly_cost.Series[4].Gallery = ChartFX.WebForms.Gallery.Bar;

			chart_monthly_cost.Series[0].Text = "주유비용";
			chart_monthly_cost.Series[1].Text = "정비비용";
			chart_monthly_cost.Series[2].Text = "주차·통행료";
			chart_monthly_cost.Series[3].Text = "세차비용";
			chart_monthly_cost.Series[4].Text = "합계";

			//검색
			lo_dp.AddProperty("ls_car_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_search_dt_from", typeof(string), kw2);
			lo_dp.AddProperty("ls_search_dt_to", typeof(string), kw3);

			using (Car2012r_NT _Car2012r_NT = new Car2012r_NT())
			{
				lo_ds.Merge(_Car2012r_NT.GetMonthlyCost(lo_dp));
				li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CAR_REPORT");
			}

			if (li_count > 0)
			{
				for (int i = 0; i < li_count; i++)
				{
					//수동으로 데이터 바인딩
					chart_monthly_cost.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", i, "Refuelling_Amount");
					chart_monthly_cost.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", i, "Service_Amount");
					chart_monthly_cost.Data[2, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", i, "Traffic_Amount");
					chart_monthly_cost.Data[3, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", i, "Washing_Amount");
					chart_monthly_cost.Data[4, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", i, "Total_Amount");

					//X축 라벨링
					chart_monthly_cost.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_REPORT", i, "Base_Dt");
				}
			}
			else
			{
				chart_monthly_cost.Visible = false;
			}
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DrawMonthlyCost();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					//현재월의 12개월 이전이 2010년 01월보다 같거나 크면 활성화
					sbtn_12_m.Visible = (Convert.ToInt32(DateUtil.GetMonthEx(-12, "yyyyMM")) >= 201001);
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-5);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			ddl_search_from_year.SelectedIndex = 0;
			ddl_search_from_month.SelectedIndex = 0;
			ddl_search_to_year.SelectedIndex = ddl_search_to_year.Items.Count - 1;
			ddl_search_to_month.SelectedIndex = ddl_search_to_month.Items.Count - 1;
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}
		
		#endregion

	}
}
